﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using myomscl.Utils;
namespace myomscl.DataObjects
{
    public class NetworkOperatorDAL : SqlBaseDal
    {
        public DataTable GetNetworkOperatorList(WorkflowFilterSql filterSql)
        {
            var result = new DataTable();

            using (SqlConnection dbc = GetConnection())
            {
                try
                {
                    var cmd = dbc.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = Constants.Sp_Name_NetworkOperator_GetList;

                    var parameters = new SqlParameter[1];
                    parameters[0] = filterSql.MunicipalityId;

                    cmd.Parameters.AddRange(parameters);

                    var da = new SqlDataAdapter(cmd);
                    dbc.Open();
                    da.Fill(result);
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    dbc.Close();
                }
            }
            return result;
        }

        public EstateDataSet.NetworkOperatorDataTable GetNetworkOperatorDetails(WorkflowFilterSql filterSql)
        {
            var result = new EstateDataSet.NetworkOperatorDataTable();

            using (SqlConnection dbc = GetConnection())
            {
                try
                {
                    var cmd = dbc.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = Constants.Sp_Name_NetworkOperator_GetDetails;

                    var parameters = new SqlParameter[2];
                    parameters[0] = filterSql.NetworkOperatorId;
                    parameters[1] = filterSql.MunicipalityId;

                    cmd.Parameters.AddRange(parameters);

                    var da = new SqlDataAdapter(cmd);
                    if (dbc.State != ConnectionState.Open)
                        dbc.Open();
                    da.Fill(result);
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    dbc.Close();
                }
            }
            return result;
        }

        public void UpdateNetworkOperator(WorkflowFilterSql filterSql)
        {
            using (SqlConnection dbc = GetConnection())
            {
                try
                {
                    var cmd = dbc.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = Constants.Sp_Name_NetworkOperator_Update;

                    var parameters = new SqlParameter[18];
                    parameters[0] = filterSql.Code;
                    parameters[1] = filterSql.Name;
                    parameters[2] = filterSql.PostAddress;
                    parameters[3] = filterSql.PostCode;
                    parameters[4] = filterSql.PostCity;
                    parameters[5] = filterSql.Email;
                    parameters[6] = filterSql.Login;
                    parameters[7] = filterSql.Password;
                    parameters[8] = filterSql.Disabled;
                    parameters[9] = filterSql.MunicipalityId;
                    parameters[10] = filterSql.NetworkOperatorId;
                    parameters[11] = filterSql.FirstName;
                    parameters[12] = filterSql.LastName;
                    parameters[13] = filterSql.Phone1;
                    parameters[14] = filterSql.Phone2;
                    parameters[15] = filterSql.Notification;
                    parameters[16] = filterSql.Confirmation;
                    parameters[17] = filterSql.UseMunicipConstractor;

                    cmd.Parameters.AddRange(parameters);
                    if (dbc.State != ConnectionState.Open)
                        dbc.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    dbc.Close();
                }
            }
        }

        public int CreateNetworkOperator(WorkflowFilterSql filterSql)
        {
            using (SqlConnection dbc = GetConnection())
            {
                try
                {
                    var cmd = dbc.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = Constants.Sp_Name_NetworkOperator_Create;

                    var parameters = new SqlParameter[17];
                    parameters[0] = filterSql.Code;
                    parameters[1] = filterSql.Name;
                    parameters[2] = filterSql.PostAddress;
                    parameters[3] = filterSql.PostCode;
                    parameters[4] = filterSql.PostCity;
                    parameters[5] = filterSql.Email;
                    parameters[6] = filterSql.Login;
                    parameters[7] = filterSql.Password;
                    parameters[8] = filterSql.Disabled;
                    parameters[9] = filterSql.MunicipalityId;
                    parameters[10] = filterSql.FirstName;
                    parameters[11] = filterSql.LastName;
                    parameters[12] = filterSql.Phone1;
                    parameters[13] = filterSql.Phone2;
                    parameters[14] = filterSql.Notification;
                    parameters[15] = filterSql.Confirmation;
                    parameters[16] = new SqlParameter
                    {
                        Direction = ParameterDirection.ReturnValue,
                        SqlDbType = SqlDbType.Int
                    };

                    cmd.Parameters.AddRange(parameters);
                    if (dbc.State != ConnectionState.Open)
                        dbc.Open();
                    cmd.ExecuteNonQuery();
                    return (int)cmd.Parameters[16].Value;
                }
                catch (SqlException ex)
                {
                    //Logging exception
                    throw ex;
                }
                finally
                {
                    dbc.Close();
                }
            }
        }
    }
}
